package com.iflytek.exam.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.iflytek.exam.service.ICreateExamService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.iflytek.common.annotation.Log;
import com.iflytek.common.core.controller.BaseController;
import com.iflytek.common.core.domain.AjaxResult;
import com.iflytek.common.enums.BusinessType;
import com.iflytek.exam.domain.ExamSet;
import com.iflytek.exam.service.IExamSetService;
import com.iflytek.common.utils.poi.ExcelUtil;
import com.iflytek.common.core.page.TableDataInfo;

/**
 * 试卷属性设置Controller
 *
 * @author Dogdog
 * @date 2024-07-09
 */
@RestController
@RequestMapping("/exam/examSet")
public class ExamSetController extends BaseController {
    @Autowired
    private IExamSetService examSetService;
    @Autowired
    private ICreateExamService createExamService;

    /**
     * 查询试卷属性设置列表
     */
    @PreAuthorize("@ss.hasPermi('exam:examSet:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamSet examSet) {
        startPage();
        List<ExamSet> list = examSetService.selectExamSetList(examSet);
        return getDataTable(list);
    }

    /**
     * 导出试卷属性设置列表
     */
    @PreAuthorize("@ss.hasPermi('exam:examSet:export')")
    @Log(title = "试卷属性设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamSet examSet) {
        List<ExamSet> list = examSetService.selectExamSetList(examSet);
        ExcelUtil<ExamSet> util = new ExcelUtil<ExamSet>(ExamSet.class);
        util.exportExcel(response, list, "试卷属性设置数据");
    }

    /**
     * 获取试卷属性设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:examSet:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ExamSet examSet = examSetService.selectExamSetById(id);
        return success(examSet);
    }

    /**
     * 获取试卷属性设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:examSet:query')")
    @GetMapping(value = "/get/{examId}")
    public AjaxResult getInfoByExamId(@PathVariable("examId") Long examId) {
        ExamSet examSet = examSetService.selectExamSetByExamId(examId);
        return success(examSet);
    }
    /**
     * 新增试卷属性设置
     */
    @PreAuthorize("@ss.hasPermi('exam:examSet:add')")
    @Log(title = "试卷属性设置", businessType = BusinessType.INSERT)
    @PostMapping("/{uuid}")
    public AjaxResult add(@RequestBody ExamSet examSet, @PathVariable String uuid) {
        //uuid找到了
        //先通过uuid找到试卷的id
        Long examId = createExamService.getExamIdByUuid(uuid);
        return toAjax(examSetService.insertExamSet(examSet, examId));
    }

    /**
     * 修改试卷属性设置
     */
    @PreAuthorize("@ss.hasPermi('exam:examSet:edit')")
    @Log(title = "试卷属性设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamSet examSet) {
        return toAjax(examSetService.updateExamSet(examSet));
    }

    /**
     * 删除试卷属性设置
     */
    @PreAuthorize("@ss.hasPermi('exam:examSet:remove')")
    @Log(title = "试卷属性设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examSetService.deleteExamSetByIds(ids));
    }
}
